/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_rr_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    mpfr_set_d(thr_rr0, real(v->v.cdd), thr_rnd);
	    if (mpfr_equal_p(thr_rr0, mpc_realref(r0.v.mpc))) {
		mpfr_set_d(thr_rr0, imag(v->v.cdd), thr_rnd);
		r0.v.i = !mpfr_equal_p(thr_rr0, mpc_imagref(r0.v.mpc));
	    }
	    else
		r0.v.i = 1;
	    break;
	case t_cqq:		case t_sqq:
	    mpfr_set_q(thr_rr0, cqq_realref(v->v.cqq), thr_rnd);
	    if (mpfr_equal_p(thr_rr0, mpc_realref(r0.v.mpc))) {
		mpfr_set_q(thr_rr0, cqq_imagref(v->v.cqq), thr_rnd);
		r0.v.i = !mpfr_equal_p(thr_rr0, mpc_imagref(r0.v.mpc));
	    }
	    else
		r0.v.i = 1;
	    break;
	case t_mpc:
	    r0.v.i = !mpfr_equal_p(mpc_realref(v->v.mpc),
				   mpc_realref(r0.v.mpc)) ||
		     !mpfr_equal_p(mpc_imagref(v->v.mpc),
				   mpc_imagref(r0.v.mpc));
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_rr_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    mpfr_set_d(thr_rr0, real(v->v.cdd), thr_rnd);
	    if (mpfr_equal_p(thr_rr0, mpc_realref(r0.v.mpc))) {
		mpfr_set_d(thr_rr0, imag(v->v.cdd), thr_rnd);
		r0.v.i = mpfr_equal_p(thr_rr0, mpc_imagref(r0.v.mpc));
	    }
	    else
		r0.v.i = 0;
	    break;
	case t_cqq:		case t_sqq:
	    mpfr_set_q(thr_rr0, cqq_realref(v->v.cqq), thr_rnd);
	    if (mpfr_equal_p(thr_rr0, mpc_realref(r0.v.mpc))) {
		mpfr_set_q(thr_rr0, cqq_imagref(v->v.cqq), thr_rnd);
		r0.v.i = mpfr_equal_p(thr_rr0, mpc_imagref(r0.v.mpc));
	    }
	    else
		r0.v.i = 0;
	    break;
	case t_mpc:
	    r0.v.i = mpfr_equal_p(mpc_realref(v->v.mpc),
				  mpc_realref(r0.v.mpc)) &&
		     mpfr_equal_p(mpc_imagref(v->v.mpc),
				  mpc_imagref(r0.v.mpc));
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_rr_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_add_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_add_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_add_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_add_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_add_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_add_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_add_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_add_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_cc(thr_c, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_sub_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_sub_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_sub_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_sub_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_sub_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_sub_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_sub_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_sub_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_cc(thr_c, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_mul_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_mul_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_mul_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_mul_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_mul_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_mul_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_mul_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_mul_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    mpc_mul(thr_c, v->v.mpc, r0.v.mpc, thr_rndc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_div_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_div_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_div_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_div_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_div_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_div_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_div_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_div_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_cc(thr_c, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_atan2_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_atan2_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_atan2_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_atan2_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_atan2_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_atan2_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_atan2_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_atan2_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_cc(thr_c, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    ecc_pow_i_cc(thr_c, 0, r0.v.mpc);
	    break;
	case t_int:
	    ecc_pow_i_cc(thr_c, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    ecc_pow_d_cc(thr_c, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    ecc_pow_z_cc(thr_c, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    ecc_pow_q_cc(thr_c, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    ecc_pow_r_cc(thr_c, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    ecc_pow_dd_cc(thr_c, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    ecc_pow_qq_cc(thr_c, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_cc(thr_c, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    if (mpfr_zero_p(thr_ri))
	set_rr();
    else
	set_rrr();
}

void
evm_rr_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_abs_cc(thr_rr, r0.v.mpc);
	    break;
	case t_int:
	    er_hypot_i_cc(thr_rr, v->v.i, r0.v.mpc);
	    break;
	case t_float:
	    er_hypot_d_cc(thr_rr, v->v.d, r0.v.mpc);
	    break;
	case t_mpz:		case t_shz:
	    er_hypot_z_cc(thr_rr, v->v.mpz, r0.v.mpc);
	    break;
	case t_mpq:		case t_shq:
	    er_hypot_q_cc(thr_rr, v->v.mpq, r0.v.mpc);
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_cc(thr_rr, v->v.mpr, r0.v.mpc);
	    break;
	case t_cdd:
	    er_hypot_dd_cc(thr_rr, v->v.cdd, r0.v.mpc);
	    break;
	case t_cqq:		case t_sqq:
	    er_hypot_qq_cc(thr_rr, v->v.cqq, r0.v.mpc);
	    break;
	case t_mpc:		case t_shc:
	    /* sqrt(a*conj(a) + b*conj(b)) */
	    er_hypot_cc_cc(thr_rr, v->v.mpc, r0.v.mpc);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_mpc(v->v.o, r0.v.mpc, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    chk_rr();
}
